package com.blb.day20231116redis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.day20231116redis.pojo.ResponseResult;
import com.blb.day20231116redis.pojo.Score;
import com.blb.day20231116redis.pojo.Student;
import com.blb.day20231116redis.service.ScoreService;
import com.blb.day20231116redis.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("分数接口")
@RestController
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "查询所有分数",tags = "全部查询")
    @GetMapping("scores")
    public ResponseResult<List<Score>> findScores(){
        List<Score> scores = scoreService.getAll();
        return ResponseResult.ok(scores);
    }

    @ApiOperation(value = "查询单个分数",tags = "通过id查询")
    @GetMapping("score/{id}")
    public ResponseResult<Score> findScoreById(@ApiParam("分数id") @PathVariable Long id){
        Score score = scoreService.getById(id);
        return ResponseResult.ok(score);
    }

    @GetMapping("scores-page")
    public ResponseResult<Page<Score>> findScoresPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                      @RequestParam(required = false,defaultValue = "5") Long size){
        IPage<Score> page = scoreService.getScorePage(current, size);
        return ResponseResult.ok(page);
    }

    @PostMapping("score")
    public ResponseResult<String> addScore(@RequestBody Score score){
        scoreService.save(score);
        return ResponseResult.ok("ok");
    }

    @PutMapping("score")
    public ResponseResult<String> updateScore(@RequestBody Score score){
        scoreService.updateById(score);
        return ResponseResult.ok("ok");
    }


    @DeleteMapping("score/{id}")
    public ResponseResult<String> deleteScore(@PathVariable Long id){
        scoreService.removeById(id);
        return ResponseResult.ok("ok");
    }
}
